/// Creator:      gaoruishan
/// CreatDate:    2025-05-23
/// Description:  床位图和入院分床相关
Class Nur.MNISV3.Service.Bed Extends %RegisteredObject [ Not ProcedureBlock ]
{

/// 所管床设置
ClassMethod SetManagedBedsV4(bedIdStr As %String, statusStr As %String, wardId As %String, userId As %String) As %String
{
    n (bedIdStr,statusStr,wardId,userId)
    s $zt="errSetManagedBedsV4"
    s ^GRS("SetManagedBedsV4")=$lb(bedIdStr,statusStr,wardId,userId)
    s msg="成功",msgcode="999999",status="0" //定义初始值
    k ^DHCNurManagedBedsSet("PDASet",userId,wardId)
    s l=$l(bedIdStr,"^")
    for i=1:1:l
    {
        s bedId=$p(bedIdStr,"^",i)
        s bedstatus=$p(statusStr,"^",i)
        continue:bedId=""
        s ^DHCNurManagedBedsSet("PDASet",userId,wardId,bedId)=bedstatus
    }   
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,msgcode,status)
errSetManagedBedsV4
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100018","-1")
}

/// 查询在床患者列表
/// w ##class(Nur.MNISV3.Service.Bed).GetPatListSelectV4("3","20665","OrderSheetFragment")
/// w ##class(Nur.MNISV3.Service.Bed).GetPatListSelectV4("3","20665","OrderExecute")
ClassMethod GetPatListSelectV4(wardId As %String, userId As %String, module = "") As %String
{
    s ^GRS("GetPatListSelectV4")=$lb(wardId,userId,module)
    s type="inBed"
    i module="TaskOverviewFragment"  s type="manageInBed" ////只查在床患者 且设置所管床
    q ##class(Nur.MNISV3.Service.Bed).GetBedListSelectV4(wardId,userId,type)
}

/// 获取所管床列表
/// w ##class(Nur.MNISV3.Service.Bed).GetBedListSelectV4("3","20665","SettingNewFragment")
ClassMethod GetBedListSelectV4(wardId As %String, userId As %String, module = "") As %String
{
    n (wardId,userId,module)
    s $zt="errGetBedListSelectV4"
    s ^GRS("GetBedListSelectV4",module_"-")=$lb(wardId,userId,module)
    // 设置页面 和医嘱闭环---全查询床位列表
    i ($g(module)="SettingNewFragment")||($g(module)="OrderExecute") s module=""
    s msg="成功",msgcode="999999",Status="0" //定义初始值
    s json=##class(%ArrayOfDataTypes).%New()
    s bedListObj=##class(%ResultSet).%New("Nur.MNISV3.Service.Bed:BedListQuery")
    d bedListObj.Execute(wardId,userId)
    k bedListSortArr
    s bedList=##class(%ListOfDataTypes).%New()
    while(bedListObj.Next()){
        s bedId=bedListObj.GetDataByName("bedId")
        s bedCode=bedListObj.GetDataByName("bedCode")
        s select=bedListObj.GetDataByName("select")
        s groupCode=bedListObj.GetDataByName("groupCode")
        s bedX=bedListObj.GetDataByName("bedX")
        s bedY=bedListObj.GetDataByName("bedY")
        s bedListSortArr(groupCode,bedY,bedX,bedCode)=bedCode_"^"_select_"^"_bedId
        
    }
    s groupCode="" ;按组排序
    f  s groupCode=$O(bedListSortArr(groupCode)) q:groupCode=""  d
    .s bedY=""
    .s groupArr=##class(%ArrayOfDataTypes).%New()
    .s groupList=##class(%ListOfDataTypes).%New()
    .f  s bedY=$O(bedListSortArr(groupCode,bedY)) q:bedY=""  d
    ..s bedX="" 
    ..f  s bedX=$O(bedListSortArr(groupCode,bedY,bedX)) q:bedX=""  d
    ...s cbedCode=""
    ...f  s cbedCode=$O(bedListSortArr(groupCode,bedY,bedX,cbedCode)) q:cbedCode=""  d
    ....s bedinfo=##class(%ArrayOfDataTypes).%New()
    ....s bedCode=$P(bedListSortArr(groupCode,bedY,bedX,cbedCode),"^",1)
    ....s select=$P(bedListSortArr(groupCode,bedY,bedX,cbedCode),"^",2)
    ....s bedId=$P(bedListSortArr(groupCode,bedY,bedX,cbedCode),"^",3)
    ....d bedinfo.SetAt(bedId,"bedId")
    ....d bedinfo.SetAt(bedCode_"床","bedCode") //拼接‘床’
    ....d bedinfo.SetAt(select,"select")
    ....s bedWard=$p(bedId,"||",1)
    ....s bedSub=$p(bedId,"||",2)
    ....s bedAdm=""
    ....s bedAdmSub=$o(^PAWARDA(bedWard,"BED",bedSub,"ADM",0))
    ....i bedAdmSub'="" d
    .....s bedAdm=$p(^PAWARDA(bedWard,"BED",bedSub,"ADM",bedAdmSub),"^",1)
    .....i bedAdm'="" d
    ......s patientID=$p($g(^PAADM(bedAdm)),"^",1)
    ......s sexID=$p($g(^PAPER(patientID,"ALL")),"^",7)
    ......s patSex=$p($g(^CT("SEX",sexID)),"^",2)
    ......d bedinfo.SetAt(patSex,"patSex")
    ......d bedinfo.SetAt(bedAdm,"episodeID")
    ......d bedinfo.SetAt(##class(Nur.MNIS.Service.PatientInfo).GetRegNo(bedAdm),"regNo")
    ......d bedinfo.SetAt($p($g(^PAPER(patientID,"ALL")),"^"),"name")
    ....i ($g(module)="inBed") d //只查在床患者
    .....i ($g(bedAdm)'="") d bedinfo.SetAt("","select") d groupList.Insert(bedinfo) 
    ....i ($g(module)="manageInBed") d //只查在床患者 且 所管床
    .....s sel=""
    .....i $d(^DHCNurManagedBedsSet("PDASet",userId,wardId,bedId)) s sel="1"
    .....i ($g(bedAdm)'="") d bedinfo.SetAt($g(sel),"select") d groupList.Insert(bedinfo) 
    ....i ($g(module)="") d groupList.Insert(bedinfo) //全查询床位列表
    .d groupArr.SetAt(groupCode,"groupCode")
    .d groupArr.SetAt(groupList,"groupList")
    .d bedList.Insert(groupArr)
    
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt($g(bedList),"bedList")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json)
errGetBedListSelectV4
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100018","-1")
}

/// 获取所管床列表--temp是引用传值，方便直接temp(bedSeqNo,bedY,bedX,bedCode)=episodeId ; regNo过滤 只取当前的
/// w ##class(Nur.MNISV3.Service.Bed).GetCurWardManagedBedTemp("3","20665",.temp)
/// w ##class(Nur.PDA.JSON).Encode(##class(Nur.MNISV3.Service.Bed).GetCurWardManagedBedTemp("3","20665"))
/// w ##class(Nur.PDA.JSON).Encode(##class(Nur.MNISV3.Service.Bed).GetCurWardManagedBedTemp("3","20665",.tem,"0000000009"))
/// w ##class(Nur.MNISV3.Service.Bed).GetCurWardManagedBedTemp("17","20665",.temp)
/// w ##class(Nur.PDA.JSON).Encode(##class(Nur.MNISV3.Service.Bed).GetCurWardManagedBedTemp("17","20665"))
/// w ##class(Nur.PDA.JSON).Encode(##class(Nur.MNISV3.Service.Bed).GetCurWardManagedBedTemp("17","20665"))
ClassMethod GetCurWardManagedBedTemp(wardId, userId, temp, regNo = "")
{
    n (wardId,userId,temp,regNo)
    s $zt="errGetCurrWardManagedBeds"
    s listAll=##class(%ListOfDataTypes).%New()
    s listMange=##class(%ListOfDataTypes).%New()
    k tmpAll
    k tmpMange
    s roomId=""
    f  
    {
        s roomId=$o(^PAADMi("CurrWard",wardId,roomId)) 
        q:roomId=""
        s episodeId=0 
        f  
        {
            s episodeId=$o(^PAADMi("CurrWard",wardId,roomId,episodeId)) 
            q:episodeId=""
            s pavisit=$p($g(^PAADM(episodeId)),"^",20)
            continue:pavisit'="A"
            s admType=$p(^PAADM(episodeId),"^",2)
            continue:admType="O"
            s bedSub=$p($g(^PAADM(episodeId)),"^",73)
            continue:bedSub=""
            s selectedBed=0 //是否 设置所管床
            i $D(^DHCNurManagedBedsSet("PDASet",userId,wardId,bedSub)){
               s selectedBed=^DHCNurManagedBedsSet("PDASet",userId,wardId,bedSub)
            }
            s motherAdm=$p($g(^PAADM(episodeId)),"^",75)
            i motherAdm'=""  s mumBed=$p($g(^PAADM(motherAdm)),"^",73)
            e  s mumBed=""
            i (mumBed'=""){ //母亲床 设置所管床
                i $D(^DHCNurManagedBedsSet("PDASet",userId,wardId,mumBed)) {
                    s selectedBed=^DHCNurManagedBedsSet("PDASet",userId,wardId,mumBed)
                }
            }
            s patRegNo=##class(web.DHCCLCom).GetRegNobyEpisodeID(episodeId)
            i $g(regNo)'="" { //只取传参的登记号
                 continue:(patRegNo'=regNo)
            }
            s papmiId=+$g(^PAADM(+episodeId))
            s patName=$p($g(^PAPER(papmiId,"ALL")),"^",1)
            s bedCode=$p($g(^PAWARD(+bedSub,"BED",$P(bedSub,"||",2))),"^",1)
            s bedX=$p($g(^PAWARD(+bedSub,"BED",$P(bedSub,"||",2))),"^",15)
            s bedY=$p($g(^PAWARD(+bedSub,"BED",$P(bedSub,"||",2))),"^",16)
            s bedSeqNo=+$p($g(^PAWARD(+bedSub,"BED",$P(bedSub,"||",2))),"^",24)
            s bedTypeDr=$p($g(^PAWARD(+bedSub,"BED",$P(bedSub,"||",2))),"^",2)
            continue:bedTypeDr=""
            s bedType=$P(^PAC("BEDTP",bedTypeDr),"^",1)
            s babyBedFlag=##class(Nur.MNIS.Service.Bed).BabyBedFlag(bedSub)
            i (babyBedFlag)
            {
                s existFlag=##class(Nur.MNIS.Service.Bed).IfExistPat(bedSub)
                i existFlag'=0 
                {
                   s bedX=$p($g(^PAWARD(+existFlag,"BED",$P(existFlag,"||",2))),"^",15)
                   s bedY=$p($g(^PAWARD(+existFlag,"BED",$P(existFlag,"||",2))),"^",16)
                   i (bedX="")||(bedY="") s bedX=+existFlag,bedY=$P(existFlag,"||",2)_"."_$P(bedSub,"||",2)
                }
            }
            i bedX="" s bedX=+bedSub,bedY=$P(bedSub,"||",2)
            continue:(bedX="")||(bedY="")
            continue:($G(existFlag)=0)&(babyBedFlag)
            s obj=##class(%ArrayOfDataTypes).%New()
            d obj.SetAt(bedSeqNo,"bedSeqNo")
            d obj.SetAt(bedY,"bedY")
            d obj.SetAt(bedX,"bedX")
            d obj.SetAt(bedCode,"bedCode")
            d obj.SetAt(episodeId,"episodeId")
            d listAll.Insert(obj)
            //按照床号排序
            s tmpAll(bedSeqNo,bedY,bedX,bedCode)=episodeId //所有的
            i $g(selectedBed)=1 {
                d listMange.Insert(obj)
                s tmpMange(bedSeqNo,bedY,bedX,bedCode)=episodeId //所管床
            }
        }
    }

    //有设置所管床，返回设置的；否则默认全部
    i $d(tmpMange)  {
        m temp = tmpMange //复制整个数组
        q listMange
    }else {
        m temp = tmpAll
        q listAll
    } 

errGetCurrWardManagedBeds
    s msg=$ze
    q msg
}

/// 入院分床患者列表
/// w ##class(Nur.MNISV3.Service.Bed).GetAllotInfoV4("3","8","191","20665")
ClassMethod GetAllotInfoV4(wardId, locId, linkLocId, userId)
{
    n (wardId,locId, linkLocId,userId)
    s $zt="errGetAllotInfoV4"
    s ^GRS("GetAllotInfoV4")=$lb(wardId, locId, linkLocId,userId)
    s msg="成功",msgcode="999999",Status="0" //定义初始值
    s json=##class(%ArrayOfDataTypes).%New()
    s emptyBedList=..GetEmptyBed(wardId,locId, $g(episodeId))
    s doctorList=..GetMainDoctors(linkLocId)
    s nurseList=..GetMainNurses(locId)
    s obsItems=..GetObsItems(locId)
    //调用纯净版患者列表，
    s patListObj=##class(%ResultSet).%New("Nur.MNISV3.Service.Patient:GetPatList")
    d patListObj.Execute(wardId,userId,"Ward",locId)
    s patInfoList=##class(%ListOfDataTypes).%New()
    while(patListObj.Next()){
        s patinfo=##class(%ArrayOfDataTypes).%New()
        f len=1:1:patListObj.GetColumnCount()
        {
             s ColumnName=patListObj.GetColumnName(len) 
             s Value=patListObj.GetDataByName(ColumnName)
             d patinfo.SetAt(Value,ColumnName)
        }
        s episodeId=patListObj.GetDataByName("episodeId")
        //基本患者信息
        s patinfo=##class(Nur.MNISV3.Service.Patient).GetBasicInfo(patinfo,episodeId)
        s newTransInPat=##class(web.DHCSETIMAGE).IsTransInPat(episodeId,wardId)
        s dischargeStatus=##class(web.DHCDischargeHistory).GetCurrentDischargeStatus(episodeId)
        s isFirstTrans=##class(Nur.NIS.Service.Transaction).ifTransFirstDispatchBed(episodeId)
        s bedCode=##class(Nur.NIS.Service.Base.Patient).GetBedCode(episodeId)
        s allotStatus=""
        i (bedCode'="") s allotStatus=$replace(bedCode,"床","")_"床"
        e  i (dischargeStatus'="") d
        .s lb=$lfs("E:医生出院,C:医生撤销出院,F:护士出院,R:护士召回病人,B:费用调整,T:结束费用调整",",")
        .s ll=$ll(lb)
        .f li=1:1:ll d
        ..s lg=$lg(lb,li)
        ..s l2=$lfs(lg,":")
        ..s g1=$lg(l2,1),g2=$lg(l2,2)
        ..i (dischargeStatus=g1) s allotStatus=g2,li=ll
        i (allotStatus="")&&(newTransInPat) s allotStatus="新"
        d patinfo.SetAt(allotStatus,"allotStatus")
        d patinfo.SetAt(isFirstTrans,"isFirstTrans")
        d patInfoList.Insert(patinfo)
    }
    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    d json.SetAt($g(emptyBedList),"emptyBeds")
    d json.SetAt($g(doctorList),"doctors")
    d json.SetAt($g(nurseList),"nurses")
    d json.SetAt($g(obsItems),"obsItems")
    d json.SetAt($g(patInfoList),"patients")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json) 
errGetAllotInfoV4
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100018","-1")
}

/// 直接取PC配置，如果有必要再单独复制一份
ClassMethod GetObsItems(locId, babyFlag, timePoint, isFirstTrans) As %String
{
    n (locId,babyFlag,timePoint,isFirstTrans,%session)
    s obsItems=##class(Nur.NIS.Service.VitalSign.Temperature).GetTempConfig(locId,$g(babyFlag),$g(timePoint),$g(isFirstTrans))
    s obsItemLen=obsItems.Count()
    f obsItemInd=1:1:obsItemLen {
        s obsItem=obsItems.GetAt(obsItemInd) 
        s desc=obsItem.GetAt("desc")
        s desc=$replace(desc,"(","（")
        i (desc["（") {
            s desc=$p(desc,"（")
            d obsItem.SetAt(desc,"desc")
        }
    } 
    q obsItems
}

/// 分配床位
/// w ##class(Nur.MNISV3.Service.Bed).AllotBedV4("295","2-5","10211","135","38","2","150","94")
ClassMethod AllotBedV4(episodeId, date, time, bedId, doctor, nurses, obsData, userId, locId)
{
    n (episodeId, date, time, bedId, doctor, nurses, obsData,userId,locId)
    s $zt="errAllotBedV4"
    s ^GRS("AllotBedV4")=$lb(episodeId, date, time, bedId, doctor, nurses, obsData,userId,locId)
    s msg="成功",msgcode="999999",Status="0" //定义初始值
    s json=##class(%ArrayOfDataTypes).%New()
    s dateVal=##class(websys.Conversions).DateHtmlToLogical(date)
    s timeVal=##class(websys.Conversions).TimeHtmlToLogical(time)
    s reqflag=##class(Nur.MNISV3.Service.Config).GetModuleFlag(
        ##class(Nur.MNISV3.Service.Session).GetPatHospID(episodeId)
        ,##class(Nur.MNISV3.Service.Session).GetPatLocID(episodeId)
        ,"Temperature","AllocBed_MainDoc_RequiredFlag")
    i (doctor="")&&(reqflag=1) s msg="请选择主管医生" q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"102121","-1")
    s bedStaObj=##class(Nur.InService.AppointManage).GetBedAppointStatus(bedId,$zd(+$h,3))
    s bedAvailable=bedStaObj.GetAt("Available")
    q:(bedAvailable'=1) ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson("床位已占用","102121","-1")
    s isFirstTrans=##class(Nur.NIS.Service.Transaction).ifTransFirstDispatchBed(episodeId)
    i (isFirstTrans){
        //生命体征项首次入院或拖床保存
        s ret=##class(Nur.NIS.Service.Transaction).FirstTransToBed(episodeId,obsData,bedId,doctor,nurses,userId,locId,dateVal,timeVal)
    }else{
        s ret=##class(Nur.NIS.Service.Transaction).TransToBed(episodeId,bedId,userId,doctor,nurses,dateVal,timeVal,"N")
    }
    i ret.GetAt("status")'="0" s msg=ret.GetAt("status"),msgcode="102121",Status="-1"

    d json.SetAt($g(msg),"msg")
    d json.SetAt($g(msgcode),"msgcode")
    d json.SetAt($g(Status),"status")
    q ##class(Nur.MNISV3.Common.JSON).Encode(json) 
errAllotBedV4
    s msg=$ze
    q ##class(Nur.MNISV3.Common.Utils).ReturnMsgJson(msg,"100018","-1")
}

/// 获取床位列表
/// d ##class(%ResultSet).RunQuery("Nur.MNISV3.Service.Bed","BedListQuery","36","13361")
Query BedListQuery(wardId As %String, userId As %String) As websys.Query(CONTAINID = 0, ROWSPEC = "bedId:%String,bedCode:%String,select:%String,groupCode:%String,bedX:%String,bedY:%String,seqNo:%String")
{
}

ClassMethod BedListQueryExecute(ByRef QHandle As %Library.Binary, wardId As %String, userId As %String) As %Library.Status
{
    Set repid=$I(^CacheTemp)
    set ind=1 s QHandle=$lb(0,repid,0) 
    k tmp
    s bedsub=0  f  s bedsub=$O(^PAWARD(wardId,"BED",bedsub)) q:bedsub=""  d
    .s bedCode=$p($g(^PAWARD(wardId,"BED",bedsub)),"^",1)
    .q:bedCode=""
    .s flag=$p($g(^PAWARD(wardId,"BED",bedsub)),"^",4)
    .q:flag="N"
    .s bedId=wardId_"||"_bedsub
    .s status="0" ;默认0 未选中， 1 选中
    .i $d(^DHCNurManagedBedsSet("PDASet",userId,wardId,bedId)) d ;是否 设置了所管床
    ..s status=$G(^DHCNurManagedBedsSet("PDASet",userId,wardId,bedId))
    .s bedX=$p($g(^PAWARD(wardId,"BED",bedsub)),"^",15)
    .s bedY=$p($g(^PAWARD(wardId,"BED",bedsub)),"^",16)
    .s bedTypeDr=$p($g(^PAWARD(wardId,"BED",bedsub)),"^",2)
    .q:bedTypeDr=""
    .s bedType=$P(^PAC("BEDTP",bedTypeDr),"^",1)
    .i bedX=""||(bedY="") s bedX=wardId,bedY=bedsub
    .q:(bedX="")||(bedY="")
    .s seqNo=$p($g(^PAWARD(wardId,"BED",bedsub)),"^",24)  //获取顺序号
    .;i bedType="MATERNALBABY" d
    .s babyBedFlag=..BabyBedFlag(bedId)
    .i (babyBedFlag) d
    ..s existFlag=..IfExistPat(bedId)
    ..i existFlag'=0 d
    ...s bedX1=$p($g(^PAWARD(+existFlag,"BED",$P(existFlag,"||",2))),"^",15)
    ...s bedY1=$p($g(^PAWARD(+existFlag,"BED",$P(existFlag,"||",2))),"^",16)
    ...i bedX1'="" s bedX=bedX1
    ...i bedY1'="" s bedY=bedY1
    ...s seqNo=$p($g(^PAWARD(+existFlag,"BED",$P(existFlag,"||",2))),"^",24)
    ...i bedX=""||(bedY="") s bedX=+existFlag,bedY=$P(existFlag,"||",2)_"."_bedsub // EH
    .;q:($G(existFlag)=0)&(bedType="MATERNALBABY")
    .q:($G(existFlag)=0)&(babyBedFlag)
    .i seqNo="" s seqNo="最后"
    .s tmp(seqNo,bedY,bedX,bedCode)=bedId_"^"_bedCode_"^"_status
    ;zw tmp
    s seqNo="" f  s seqNo=$O(tmp(seqNo)) q:seqNo=""  d
    .s cbedY=""  f  s cbedY=$O(tmp(seqNo,cbedY)) q:cbedY=""  d
    ..s cbedX=""  f  s cbedX=$O(tmp(seqNo,cbedY,cbedX)) q:cbedX=""  d
    ...s cbedCode=""  f  s cbedCode=$O(tmp(seqNo,cbedY,cbedX,cbedCode)) q:cbedCode=""  d
    ....s bedId=$p(tmp(seqNo,cbedY,cbedX,cbedCode),"^",1)
    ....s bedcode=$p(tmp(seqNo,cbedY,cbedX,cbedCode),"^",2)
    ....s status=$p(tmp(seqNo,cbedY,cbedX,cbedCode),"^",3)
    ....s groupCode="其他"
    ....d findBedGroup
    ....d OutRowBedList
    
    Set QHandle=$lb(0,repid,0)
    Quit $$$OK
OutRowBedList
    set Data=$lb(bedId,bedcode,status,groupCode,cbedX,cbedY,seqNo)
    Set ^CacheTemp(repid,ind)=Data
    Set ind=ind+1
findBedGroup
    s groupBedDr=$o(^CF.NUR.NIS.WardProGroupBed(0,"PacBedDr",bedId,0))
    q:groupBedDr=""
    s groupDr=$p(^CF.NUR.NIS.WardProGroupBed(groupBedDr),"^",1)
    q:groupDr=""
    s active=$p(^CF.NUR.NIS.WardProGroup(groupDr),"^",3)
    q:active'="Y"
    s groupCode=$p(^CF.NUR.NIS.WardProGroup(groupDr),"^",1)
    q
}

/// 8.5判断婴儿床
/// w ##class(Nur.MNISV3.Service.Bed).BabyBedFlag()
ClassMethod BabyBedFlag(bedID) As %String
{
    n (bedID)
    s babyBedFlag=0
    s bedWardID=+bedID,bedChl=$p(bedID,"||",2)
    q:(bedChl="") babyBedFlag
    s bedTypeID=$p($g(^PAWARD(bedWardID,"BED",bedChl)),"^",2)
    q:(bedTypeID="") babyBedFlag
    s bedTypeCode=$p($g(^PAC("BEDTP",bedTypeID)),"^",1)
    i (##class(%Dictionary.MethodDefinition).%ExistsId("Nur.NIS.Service.Base.Bed||ifBabyBed")) d
    .s ifBabyBed=##class(Nur.NIS.Service.Base.Bed).ifBabyBed(bedTypeCode)
    .i (ifBabyBed="Y") s babyBedFlag=1
    e  d
    .i (bedTypeCode="MATERNALBABY") s babyBedFlag=1
    q babyBedFlag
}

/// 判断床位上是否有患者 
/// w ##class(Nur.MNISV3.Service.Bed).IfExistPat("3||1")
ClassMethod IfExistPat(bedId)
{
    n (bedId)
    s ret=0
    i bedId="" q ret
    s wardId=$P(bedId,"||",1)
    s bedSub=$P(bedId,"||",2)
    q:(wardId="")||(bedSub="") ret
    s admsub=""
    f  s admsub=$O(^PAWARDA(wardId,"BED",bedSub,"ADM",admsub)) q:admsub=""  d
    .s episodeId=$P($G(^PAWARDA(wardId,"BED",bedSub,"ADM",admsub)),"^",1)
    .s patVisit=$p($g(^PAADM(episodeId)),"^",20)
    .q:patVisit'="A"
    .s motherEpisodeId=$p($g(^PAADM(episodeId)),"^",75)
    .i motherEpisodeId'="" d ;母亲就诊号 判断
    ..s ret=$p($g(^PAADM(motherEpisodeId)),"^",73) 
    ..i (+ret)'=(+bedId) s ret=bedId
    q ret
}

/// 获取病区空床位列表
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Bed).GetEmptyBed(3,8))
ClassMethod GetEmptyBed(wardId, locId, episodeId = "")
{
    n (wardId, locId, episodeId)
    s beds=##class(%ListOfDataTypes).%New()
    s rs=##Class(%Library.ResultSet).%New("epr.FPView:FindForFloorplan")
    d rs.Execute(wardId,"")
    while(rs.Next()){
        s bed=##class(%ArrayOfDataTypes).%New()
        s BedID=rs.GetDataByName("ID")
        s bedcode=rs.GetDataByName("BEDCode")
        s isActive=rs.GetDataByName("ActiveFlag")
        //去掉未激活的床位
        continue:isActive'="Y"
        //去掉包床等不可用床位
        s unavailreason=""  
        s unAvail=##class(web.PACBedStatusChange).IsUnavailBedStatus(BedID,.unavailreason)  
        continue:unAvail=1
        //去掉已有病人的床位
        s bedadm=$o(^PAWARDA(+wardId,"BED",+$p(BedID,"||",2),"ADM",0))
        
        continue:bedadm'=""
        s bedTypeDR=$p(^PAWARD(wardId,"BED",$P(BedID,"||",2)),"^",2)
        s bedTypeCode=$p(^PAC("BEDTP",bedTypeDR),"^",1)  
        ;continue:(bedTypeCode="MATERNALBABY")
        continue:(..BabyBedFlag(BedID))
        s ifRoomSexRestrict=##class(Nur.NIS.Service.Base.Bed).IfRoomSexRestrict(episodeId, BedID)
        i ifRoomSexRestrict'=0 s note="房间只允许住同性别病人"
        d bed.SetAt(BedID,"bedId")
        d bed.SetAt(bedcode,"bedCode")
        d bed.SetAt(ifRoomSexRestrict,"noClickFlag")
        d bed.SetAt($g(note),"note")
        d beds.Insert(bed)
    }
    q beds
}

/// 获取病区主管护士
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Bed).GetMainNurses(5))
ClassMethod GetMainNurses(locID) As %ListOfDataTypes
{
    n (locID)
    s mainNurses=##class(%ListOfDataTypes).%New()
    s resourceID=0
    f  s resourceID=$o(^RB("RES",0,"CTLOC",locID,resourceID)) q:resourceID=""  d
    .s careProvID=$p($G(^RB("RES",resourceID)),"^",2)
    .q:careProvID=""
    .s nurseCode=$P($G(^CTPCP(careProvID,1)),"^",1)
    .s nurseName=$P($G(^CTPCP(careProvID,1)),"^",2)
    .;q:(nurseName'[searchStr)&&(nurseCode'[searchStr)&&(searchStr'="")
    .s careProvTypeID=$P($G(^CTPCP(careProvID,1)),"^",4)
    .q:careProvTypeID=""
    .s careProvType=$P($G(^CT("CPT",careProvTypeID)),"^",4)
    .q:careProvType'="NURSE"
    .s mainNurse=##class(%ArrayOfDataTypes).%New()
    .d mainNurse.SetAt(nurseName,"name")
    .d mainNurse.SetAt(+careProvID,"ID")
    .d mainNurse.SetAt(nurseCode,"jobNo")
    .d mainNurses.Insert(mainNurse)
    q mainNurses
}

/// 获取科室医生
/// w ##class(Nur.JSON).Encode(##class(Nur.MNISV3.Service.Bed).GetMainDoctors(5))
ClassMethod GetMainDoctors(locID) As %ListOfDataTypes
{
    n (locID)
    s mainDocs=##class(%ListOfDataTypes).%New()
    s resourceID=0
    f  s resourceID=$o(^RB("RES",0,"CTLOC",locID,resourceID)) q:resourceID=""  d
    .s careProvID=$p($G(^RB("RES",resourceID)),"^",2)
    .s admRight=$p($G(^RB("RES",resourceID)),"^",20)
    .q:(admRight'="Y")
    .s docName=$p($G(^RB("RES",resourceID)),"^",17)
    .q:careProvID=""
    .s docCode=$P($G(^CTPCP(careProvID,1)),"^",1)
    .;q:(nurseName'[searchStr)&&(nurseCode'[searchStr)&&(searchStr'="")
    .s careProvTypeID=$P($G(^CTPCP(careProvID,1)),"^",4)
    .q:careProvTypeID=""
    .s activeFlag=$P($G(^CTPCP(careProvID,1)),"^",9)
    .q:activeFlag="N"
    .s careProvType=$P($G(^CT("CPT",careProvTypeID)),"^",4)
    .q:careProvType'="DOCTOR"
    .s mainDoc=##class(%ArrayOfDataTypes).%New()
    .d mainDoc.SetAt(docName,"name")
    .d mainDoc.SetAt(+careProvID,"ID")
    .d mainDoc.SetAt(docCode,"jobNo")
    .d mainDocs.Insert(mainDoc)
    q mainDocs
}

}
